/***************************************************************************
 创建者: hualei
 开始时间: 2018.9.10
 copyright: (C) 深圳市华友高科有限公司
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2018.9.10 类的具体实现代码编写

 ***************************************************************************/
#include "ftpprogressdialog.h"
#include "ui_ftpprogressdialog.h"
#include "QDebug"

FtpProgressDialog::FtpProgressDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FtpProgressDialog)
{
    ui->setupUi(this);
    connect(ftpManage, SIGNAL(updateProgress(qint64,qint64)), this, SLOT(updateProgressSlot(qint64,qint64)));
    connect(ftpManage, SIGNAL(currentGetFile(QString)), this, SLOT(currentGetFileSlot(QString)));
    connect(ftpManage, SIGNAL(currentPutFile(QString)), this, SLOT(currentPutFileSlot(QString)));
    m_nTime = 0;
}

FtpProgressDialog::~FtpProgressDialog()
{
    delete ui;
}

QString FtpProgressDialog::conversionSize(qint64 totalBytes)
{
    QString strUnit;
    double dSize = totalBytes * 1.0;
    if (dSize <= 0)
    {
        dSize = 0.0;
    }
    else if (dSize < 1024.0)
    {
        strUnit = "Bytes";
    }
    else if (dSize < 1024.0 * 1024.0)
    {
        dSize /= 1024.0;
        strUnit = "KB";
    }
    else if (dSize < 1024.0 * 1024.0 * 1024.0)
    {
        dSize /= (1024.0 * 1024.0);
        strUnit = "MB";
    }
    else
    {
        dSize /= (1024.0 * 1024.0 * 1024.0);
        strUnit = "GB";
    }

    return QString("%1 %2").arg(QString::number(dSize, 'f', 2)).arg(strUnit);
}

QString FtpProgressDialog::transferSpeed(double speed)
{
    QString strUnit;
    if (speed <= 0)
    {
        speed = 0;
        strUnit = "Bytes/S";
    }
    else if (speed < 1024)
    {
        strUnit = "Bytes/S";
    }
    else if (speed < 1024 * 1024)
    {
        speed /= 1024;
        strUnit = "KB/S";
    }
    else if (speed < 1024 * 1024 * 1024)
    {
        speed /= (1024 * 1024);
        strUnit = "MB/S";
    }
    else
    {
        speed /= (1024 * 1024 * 1024);
        strUnit = "GB/S";
    }

    QString strSpeed = QString::number(speed, 'f', 2);
    return QString("%1 %2").arg(strSpeed).arg(strUnit);
}

void FtpProgressDialog::on_sureBtn_clicked()
{
    this->close();
}

void FtpProgressDialog::updateProgressSlot(qint64 readBytes, qint64 totalBytes)
{
    // 总时间
    int nTime = downloadTime.elapsed();
    // 本次下载所用时间
    nTime -= m_nTime;
    // 下载速度
    double dBytesSpeed = (readBytes * 1000.0) / nTime;
    ui->transfer_Value_Label->setText(transferSpeed(dBytesSpeed));
    ui->fileSize_Value_Label->setText(conversionSize(totalBytes));
    ui->progressBar->setMaximum(totalBytes);
    ui->progressBar->setValue(readBytes);

//    qDebug() << "totalBytes :" << totalBytes;
//    qDebug() << "ReadBytes :" << readBytes;
    // 当文件在传输过程中时，确认按钮失效，传输完成后使能
    if(readBytes == totalBytes)
    {
        ui->progressBar->setValue(totalBytes);
        ui->sureBtn->setEnabled(true);
        m_nTime = 0;
    }
    else
    {
        m_nTime = nTime;
        ui->sureBtn->setEnabled(false);
    }
}

void FtpProgressDialog::currentGetFileSlot(const QString &filePath)
{
    downloadTime.start();

    currentGetFile = filePath;
    ui->transferSpeed_label->setText(tr("下载速度:"));
    ui->fileName_Value_Label->setText(filePath);
}

void FtpProgressDialog::currentPutFileSlot(const QString &filePath)
{
    downloadTime.start();

    currentPutFile = filePath;
    ui->transferSpeed_label->setText(tr("上传速度:"));
    ui->fileName_Value_Label->setText(filePath);
}
